<?php
/*
 * 用户中心
 */

namespace Home\Controller;

//use OSS\Core\OssException;
use Think\Controller;

class UserCenterController extends Controller {

    protected $user_id = 0;
    protected $user_info = [];

    public function __construct(){
        parent::__construct();
        $this->user_id = isset($_REQUEST['user_id']) && !empty($_REQUEST['user_id'])?$_REQUEST['user_id']:1;
        if(empty($this->user_id)){
            echo '请登录账号';die;
        }

        $user_info = M("user")->where('user_id='.$this->user_id)->find();
        $this->user_info = $user_info;
        if(empty($this->user_info)){
            echo '账号异常';die;
        }

    }


    public function index(){
        $this->display();
    }

    //续费页面
    public function distPay(){
        $this->user_info['expiration_time_text'] = !empty($this->user_info['expiration_time'])?date('Y-m-d',$this->user_info['expiration_time']):'--';

        $oConfigModel       = new \Common\Model\ConfigModel;
        $oWxPayLogic    =   new \Common\Logic\WxPayLogic;


        $aMoneyDate = $oConfigModel->getMonthMoney($this->user_info['port']);

        $this->assign('month_money',$aMoneyDate['month_pay_prices']);//月
        $this->assign('quarter_money',$aMoneyDate['quarter_pay_prices']);//季度
        $this->assign('year_money',$aMoneyDate['year_pay_prices']);//年

        $this->assign('user_info',$this->user_info);


        $jssdk = $oWxPayLogic->JssdkConfig();
        $this->assign('jssdk',$jssdk);


        $this->display();

    }

    //续费生成订单
    public function distPaySubmit(){

        if(isset($_POST['type']) && !empty($_POST['type'])){
            $oConfigModel       = new \Common\Model\ConfigModel;
            $oWxPayLogic    =   new \Common\Logic\WxPayLogic;


            $aMoneyDate = $oConfigModel->getMonthMoney($this->user_info['port']);


            $month_count = 0;
            $pay_prices = 0;
            $total_prices = 0;

            switch($_POST['type']){
                case 1://年
                    $month_count = 12;
                    $pay_prices = $aMoneyDate['year_pay_prices'];
                    $total_prices = $aMoneyDate['year_total_prices'];
                    break;
                case 2://季度
                    $month_count = 3;
                    $pay_prices = $aMoneyDate['quarter_pay_prices'];
                    $total_prices = $aMoneyDate['quarter_total_prices'];
                    break;
                case 3://月
                    $month_count = 1;
                    $pay_prices = $aMoneyDate['month_pay_prices'];
                    $total_prices = $aMoneyDate['month_total_prices'];
                    break;
                default:
                    break;
            }

            if(empty($month_count) || empty($pay_prices)  || empty($total_prices)){
                $return = ['code'=>-1,'msg'=>'参数错误!!!'];
                $this->ajaxReturn($return);
            }else{

                //计算每个端口均价
                $price = bcdiv($pay_prices,$this->user_info['port'],2);

                $oOrderModel     = new \Common\Model\OrderModel;

                $data['user_id'] = $this->user_id;
                $data['order_type'] = $oOrderModel::ORDER_TYPE_XU;
                $data['count'] = $month_count;
                $data['port_count'] = $this->user_info['port'];
                $data['price'] = $price;
                $data['total_prices'] = $total_prices;
                $data['pay_prices'] = $pay_prices;
                $data['order_no'] = $oOrderModel->getOrderNo();

//                $notify_url = '/home/NotifyUrl/distPayCallback';
//                $aPrepayInfo = $oWxPayLogic->getPrepayId($data['order_no'],$data['price'],$this->user_info['openid'],$notify_url);
//                if($aPrepayInfo['code'] == 1 && isset($aPrepayInfo['prepay_id'])){
//                    if($oOrderModel->add($data)){
//                        $return = ['code'=>1,'msg'=>'支付成功!!!','prepay_id'=>$aPrepayInfo['prepay_id']];
//                        $this->ajaxReturn($return);
//                    }else{
//                        $return = ['code'=>-1,'msg'=>'数据库插入失败!!!'];
//                        $this->ajaxReturn($return);
//                    }
//                }else{
//                    $return = ['code'=>-1,'msg'=>'获取不到prepay_id!!!'];
//                    $this->ajaxReturn($return);
//                }

                if($oOrderModel->add($data)){
                    $return = ['code'=>1,'msg'=>'支付成功!!!'];
                    $this->ajaxReturn($return);
                }else{
                    $return = ['code'=>-1,'msg'=>'数据库插入失败!!!'];
                    $this->ajaxReturn($return);
                }

            }
        }
        $this->display();
    }


    //扩容页面
    public function distPayAdd(){
        $oConfigModel       = new \Common\Model\ConfigModel;


        $this->user_info['expiration_time_text'] = !empty($this->user_info['expiration_time'])?date('Y-m-d',$this->user_info['expiration_time']):'--';
        $this->assign('user_info',$this->user_info);

        $oWxPayLogic    =   new \Common\Logic\WxPayLogic;
        $jssdk = $oWxPayLogic->JssdkConfig();

        $this->assign('jssdk',$jssdk);

        //扩容默认按1个端口算
        $aMoneyDate = $oConfigModel->getMonthMoney(1);

        $day_price = $aMoneyDate['day_total_prices'];//一天一端口价格;
        $this->assign('day_price',$day_price);


        $this->display();
    }


    //扩容生成订单
    public function distPayAddSubmit(){
        $oConfigModel       = new \Common\Model\ConfigModel;
        $oOrderModel     = new \Common\Model\OrderModel;
        $oWxPayLogic    =   new \Common\Logic\WxPayLogic;

        if(isset($_POST['num']) && !empty($_POST['num'])){

            if(!is_numeric($_POST['num']) || $_POST['num'] < 1 || strpos($_POST['num'],".")!==false){
                $return = ['code'=>-1,'msg'=>'请输入正确的数字，数字必须是正整数!!!'];
                $this->ajaxReturn($return);
            }

            if($this->user_info['expiration_time']<time()){
                $return = ['code'=>-1,'msg'=>'您的端口已过期，请先续费再进行扩容操作!!!'];
                $this->ajaxReturn($return);
            }

            //扩容默认按1个端口算
            $aMoneyDate = $oConfigModel->getMonthMoney(1);
            $day_price = $aMoneyDate['day_total_prices'];//一天一端口价格;

            $newTime = $this->user_info['expiration_time']-time();
            $day_count = round($newTime / 86400);//需要买多少天,一天=86400秒
            $total_prices = bcmul($day_price,$_POST['num'],2);//$day_count天*$_POST['num']扩容端口数=一天费用
            $total_prices = bcmul($total_prices,$day_count,2);//一天费用*$day_count需要续费的日期=总价

            $pay_prices = $total_prices;

            $data = [];
            $data['user_id'] = $this->user_id;
            $data['order_type'] = $oOrderModel::ORDER_TYPE_PORT;
            $data['count'] = $day_count;
            $data['port_count'] = $_POST['num'];
            $data['price'] = $day_price;//单天单接口费用
            $data['total_prices'] = $total_prices;
            $data['pay_prices'] = $pay_prices;
            $data['order_no'] = $oOrderModel->getOrderNo();


//            $notify_url = '/home/NotifyUrl/distPayAddCallback';
//            $aPrepayInfo = $oWxPayLogic->getPrepayId($data['order_no'],$data['price'],$this->user_info['openid'],$notify_url);
//            if($aPrepayInfo['code'] == 1 && isset($aPrepayInfo['prepay_id'])){
//                if($oOrderModel->add($data)){
//                    $return = ['code'=>1,'msg'=>'支付成功!!!','prepay_id'=>$aPrepayInfo['prepay_id']];
//                    $this->ajaxReturn($return);
//                }else{
//                    $return = ['code'=>-1,'msg'=>'数据库插入失败!!!'];
//                    $this->ajaxReturn($return);
//                }
//            }else{
//                $return = ['code'=>-1,'msg'=>'获取不到prepay_id!!!'];
//                $this->ajaxReturn($return);
//            }

            if($oOrderModel->add($data)){
                $return = ['code'=>1,'msg'=>'支付成功!!!'];
                $this->ajaxReturn($return);
            }else{
                $return = ['code'=>-1,'msg'=>'数据库插入失败!!!'];
                $this->ajaxReturn($return);
            }

        }

        $return = ['code'=>-1,'msg'=>'参数错误!!!'];
        $this->ajaxReturn($return);


    }


}